package com.beiding.t;

import com.beiding.jsonanalyzer.analyze.ResourceUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author 丁常磊
 * @date 2021/12/21 14:36
 */

public class 比对两个war包的差异 {
    public static void main(String[] args) throws IOException {
//
//        File fileA = new File("C:\\Users\\lenovo\\Desktop\\文件比对\\ocp-common\\uat");
//        File fileB = new File("C:\\Users\\lenovo\\Desktop\\文件比对\\ocp-common\\生产");
 /*       File fileA = new File("C:\\Users\\lenovo\\Desktop\\文件比对\\omp-event-core\\uat");
        File fileB = new File("C:\\Users\\lenovo\\Desktop\\文件比对\\omp-event-core\\生产");*/
        File fileA = new File("C:\\Users\\lenovo\\Desktop\\文件比对\\uat");
        File fileB = new File("C:\\Users\\lenovo\\Desktop\\文件比对\\生产");

        List<A> l1 = new ArrayList<>();
        List<A> l2 = new ArrayList<>();

        deep(l1, fileA, "");
        deep(l2, fileB, "");

        Map<String, File> m1 = new HashMap<>();
        Map<String, File> m2 = new HashMap<>();

        hp(m1, l1);
        hp(m2, l2);

        Iterator<Map.Entry<String, File>> iterator = m1.entrySet().iterator();

        while (iterator.hasNext()) {

            Map.Entry<String, File> next = iterator.next();

            File f2 = m2.remove(next.getKey());

            //比对文件是否相等

            if (f2 == null) {
                System.out.println("B缺失文件 => " + fileB.getPath());
                System.out.println(next.getKey());
                System.out.println("文件A   "+next.getValue().getPath());

            } else {
                String a1 = ResourceUtils.read(next.getValue());
                String a2 = ResourceUtils.read(f2);
                if (!a1.equals(a2)) {
                    System.out.println("文件不同");
                    System.out.println(next.getKey());
                    System.out.println("文件A   "+next.getValue().getPath());
                    System.out.println("文件B   "+f2.getPath());
                }else{
//                    System.out.println("文件相同");
//                    System.out.println(next.getLeft());
                }

            }

            iterator.remove();

        }

        if (m2.size() > 0) {
            m2.forEach((k, v) -> {
                System.out.println("A缺失文件 => " + fileA.getPath());
                System.out.println(k);
                System.out.println("文件B   "+v.getPath());

            });
        }
    }

    public static void hp(Map<String, File> m, List<A> a) {

        for (A a1 : a) {
            m.put(a1.name, a1.file);
        }
    }

    public static void deep(List<A> all, File file, String pn) {

        if (file.isFile()) {
            A a = new A();
            a.file = file;
            a.name = pn;
            all.add(a);
        } else if (file.isDirectory()) {
            File[] listFiles = file.listFiles();

            if (listFiles != null) {
                for (File listFile : listFiles) {
                    String name = listFile.getName();
                    deep(all, listFile, pn + "/" + name);
                }
            }
        }

    }


    public static class A {
        String name;
        File file;
    }

}
